package com.example.lin.day9_10.day_9_10;

import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.lin.day9_10.day_9_10.application.MyApp;
import com.example.lin.day9_10.day_9_10.utils.OkUtils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    /**
     * 网络请求框架 OkHttp Retrofit Volley
     *
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
    }

    //
    public void test1(View view) {
        String url = "http://location.ximalaya.com/location-web/location?latitude=22.535456&longitude=114.013493";
        OkUtils.get(url, new OkUtils.OnOkCallback() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String response) {
                Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void testk(View view) {
        final String registerUrl = "http://192.168.3.143:8080/home/register";
        HashMap<String, String> map = new HashMap<>();
        map.put("userName", "lkjhlkhjk");
        map.put("psw", "123456");
        OkUtils.post(registerUrl, map, new OkUtils.OnOkCallback() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String response) {
                Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show();
            }
        });
    }

    //
//    public void test2(View view) {
////        String url = "http://location.ximalaya.com/location-web/location?latitude=22.535456&longitude=114.013493";
//        String url = "http://adse.ximalaya.com/ting/loading/ts-1535958550381?androidId=70efed795448c530f89cb353813dd6de6bcd7461&appid=0&device=android&name=loading&network=wifi&operator=2&userAgent=Mozilla/5.0%20(Linux;%20Android%208.0.0;%20BLA-AL00%20Build/HUAWEIBLA-AL00;%20wv)%20AppleWebKit/537.36%20(KHTML,%20like%20Gecko)%20Version/4.0%20Chrome/63.0.3239.111%20Mobile%20Safari/537.36&version=6.5.16&xt=1535958550381";
//
//        OkUtils.get(url, new OkUtils.OnOkCallback() {
//            @Override
//            public void onFailure(IOException e) {
//
//            }
//
//            @Override
//            public void onResponse(String response) {
//
//            }
//        });
//
//
//    }
//
//    public static final MediaType TYPE_FILE = MediaType.parse("application/octet-stream");
//
//    public void upload(View view) {
//        String uploadFileUrl = "http://192.168.3.143:8080/home/upload";
//
//        File sdRoot = Environment.getExternalStorageDirectory();
//
//        File file1 = new File(sdRoot, "g3.jpg");//需要上传的文件
//        File file2 = new File(sdRoot, "g5.jpg");//需要上传的文件
//
//        RequestBody fileBody1 = RequestBody.create(TYPE_FILE, file1);
//        RequestBody fileBody2 = RequestBody.create(TYPE_FILE, file2);
//
//        MultipartBody multipartBody = new MultipartBody.Builder()
//                .addFormDataPart("f1", file1.getName(), fileBody1)
//                .addFormDataPart("f2", file2.getName(), fileBody2)
//                .addFormDataPart("content", "今天天气好凉快啊!")
//                .build();
//
//        Request request = new Request.Builder().url(uploadFileUrl).post(multipartBody).build();
//
//        MyApp.getOkClient().newCall(request).enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                Log.e(TAG, "onFailure: ");
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                Log.d(TAG, "onResponse: " + response.body().string());
//            }
//        });
//
//    }
//
    public void download(View view) {
//        String url = "http://192.168.3.143:8080/home/download?filename=Fade.mp3";
        String url = "http://192.168.3.143:8080/file/Fade.mp3";
        File dir = getExternalFilesDir(null);
        OkUtils.download(url, dir.getAbsolutePath(), new OkUtils.OnOkDownloadCallback() {
            @Override
            public void onProgress(float progress) {
                Log.d(TAG, "onProgress: " + progress);
            }

            @Override
            public void onResponse(String filepath) {
                Toast.makeText(MainActivity.this, "成功!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(IOException e) {
                Toast.makeText(MainActivity.this, "失败!", Toast.LENGTH_SHORT).show();
            }
        });
    }


}
